package com.viu.technology.config.datasource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@MapperScan("com.viu.technology.mapper")
public class MybatisConfig extends HikariConfig{

    private static Logger log = org.slf4j.LoggerFactory.getLogger(MybatisConfig.class);

    @PostConstruct
    public void init() {
        log.info("正在初始化容器");
    }

    @PreDestroy
    public void destroy() {
        log.info("容器被销毁后");
    }

    @Autowired
    private Environment env;

    //使用Hikaricp数据源
    @Bean("datasource1")
    @ConfigurationProperties(prefix = "datasource1.jdbc")
    public DataSource dataSource1() {
        log.info("开始配置DataSource数据源1");
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(env.getProperty("datasource1.jdbc.driverClassName"));
        dataSource.setJdbcUrl(env.getProperty("datasource1.jdbc.url"));
        dataSource.setUsername(env.getProperty("datasource1.jdbc.username"));
        dataSource.setPassword(env.getProperty("datasource1.jdbc.password"));
        dataSource.setMaximumPoolSize(100);
        dataSource.setPoolName("hikari-");
        return dataSource;
    }

    //配置第二个数据源
    @Bean("datasource2")
    @ConfigurationProperties(prefix = "datasource2.jdbc")
    public DataSource dataSource2() {
        log.info("开始配置DataSource数据源2");
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(env.getProperty("datasource2.jdbc.driverClassName"));
        dataSource.setJdbcUrl(env.getProperty("datasource2.jdbc.url"));
        dataSource.setUsername(env.getProperty("datasource2.jdbc.username"));
        dataSource.setPassword(env.getProperty("datasource2.jdbc.password"));
        return dataSource;
    }


    //配置多数据源，使用ThreadLocal对对应的线程保存不用的类型，根据类型获取不同的数据源
    @Bean("datasource")
    @Primary
    public DynamicDataSource dataSource(@Qualifier("datasource1") DataSource dataSource1, @Qualifier("datasource2") DataSource dataSource2) {
        Map<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DatabaseType.DATASOURCE1, dataSource1);
        targetDataSource.put(DatabaseType.DATASOURCE2, dataSource2);

        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSource);
        dataSource.setDefaultTargetDataSource(dataSource1);
        return dataSource;
    }

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactory( DataSource dataSource) throws Exception {
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(dataSource);
        fb.setTypeAliasesPackage("com.viu.technology.po");
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/*.xml"));
        return fb.getObject();
    }


}
